package com.wwz.leetcode;

import java.util.*;

/**
 * <p>Description:判断子序列</p>
 * <p>Copyright: Copyright (c)2020</p>
 * <p>Company: Tope</p>
 * <P>Created Date :2020-07-27</P>
 * <P>@version 1.0</P>
 *
 * 示例 1:
 s = "abc", t = "ahbgdc"

 返回 true.

 示例 2:
 s = "axc", t = "ahbgdc"

 返回 false.

 */
public class LeetCode392 {

    public static void main(String[] args) {

        String s="abc";
        String bt="afrcbdkj";
        int target=0;
        for(char c:s.toCharArray()){
            int pos=bt.indexOf(c,target);
            System.out.println(pos);
            target=pos+1;
        }
        String t ="ahbgdc";
        System.out.println(t.length());
        boolean subsequence = isSubsequence("acb",t);
        System.out.println(subsequence);
    }

    public static boolean isSubsequence(String s, String t) {
        char[] chars = t.toCharArray();
        Map<String,Object> parentMap = new HashMap<String,Object>();
        for (int i = 0; i < chars.length; i++) {
            parentMap.put(String.valueOf(i),String.valueOf(chars[i]));
        }
        char[] chars2 = s.toCharArray();
        Map<String,Object> childMap = new HashMap<String,Object>();
        for (int k = 0; k < chars2.length; k++) {
            childMap.put(String.valueOf(k),String.valueOf(chars2[k]));
        }
        String x ="";
        ArrayList<Integer> xx = new ArrayList<Integer>();
        for (Map.Entry<String, Object> entry:childMap.entrySet()) {
            if(parentMap.containsValue(entry.getValue())){
                for(Map.Entry<String, Object> e : parentMap.entrySet()){
                    if(entry.getValue().equals(e.getValue())){
                        x=e.getKey();
                        xx.add(Integer.valueOf(x));
                    }
                }
                Integer p = Collections.min(xx);
                xx.clear();
                //存入中间map
                Map<String,Object> midMap = new HashMap<String,Object>();
                midMap.putAll(parentMap);
                Iterator<String> iter = midMap.keySet().iterator();
                while(iter.hasNext()){
                    String key = iter.next();
                    if(Integer.valueOf(key)<=Integer.valueOf(p)){
                        iter.remove();
                    }
                }
                parentMap.clear();
                //重新排布
                int i =0;
                Map<String, Object> sMap = sortMap(midMap);

                for(Map.Entry<String, Object> sm:sMap.entrySet()){
                    parentMap.put(String.valueOf(i),sm.getValue());
                    i++;
                }
                System.out.println("parentMap"+parentMap);

            }else {
                return false;
            }
        }
        return true;

    }

    //按照key的大小排序
    public static Map<String, Object> sortMap(Map<String, Object> k_v) {
        List<Map.Entry<String, Object>> list = new ArrayList<Map.Entry<String, Object>>(k_v.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Object>>() {
            @Override
            public int compare(Map.Entry<String, Object> o1,
                               Map.Entry<String, Object> o2) {
                // 升序排序
                return Integer.parseInt(o1.getKey()) - Integer.parseInt(o2.getKey());
            }
        });
        Map result = new LinkedHashMap();
        for (Map.Entry<String, Object> entry : list) {
            result.put(entry.getKey(), entry.getValue());

        }

        return result;
    }
}
